package com.sicnu.resources.service.impl;


import com.sicnu.resources.bean.entity.Resource;
import com.sicnu.resources.bean.req.resource.ResourcePageReq;
import com.sicnu.resources.bean.res.Result;
import com.sicnu.resources.config.RedisCache;
import com.sicnu.resources.mapper.ResourceMapper;
import com.sicnu.resources.service.ResourceService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 资源服务实现类
 * @author 怨九己
 */
@Service
@RequiredArgsConstructor
public class ResourceServiceImpl implements ResourceService {
    private final ResourceMapper resourceMapper;

    /**
     * 根据资源ID查询资源
     * @param resourceId 资源ID
     * @return 资源对象
     */
    @Override
    public Resource findById(Integer resourceId) {
        return resourceMapper.findById(resourceId);
    }

    /**
     * 保存资源
     *
     * @param resource 资源对象
     * @return 是否保存成功
     */
    @Override
    public Result<String> save(Resource resource) {
        resourceMapper.insert(resource);
        if (resource.getResourceId() != null) {
            return Result.buildSuccess("资源保存成功");
        } else {
            return Result.buildFailure("资源保存失败");
        }
    }

    /**
     * 删除资源
     * @param resourceId 资源ID
     */
    @Override
    public Result<String> delete(Integer resourceId) {
        Resource resource = resourceMapper.findById(resourceId);
        if (resource == null) {
            return Result.buildFailure("资源不存在，删除失败");
        }
        resourceMapper.delete(resourceId);

        return Result.buildSuccess("资源删除成功");
    }

    /**
     * 根据资源名称和标签搜索资源（支持分页）
     * @param resourceName 资源名称
     * @param tag 标签
     * @param pageReq 分页请求参数
     * @return 资源列表和总数
     */
    @Override
    public Result<List<Resource>> searchResourcesByName(
            String resourceName,
            String tag,
            ResourcePageReq pageReq) {
        int offset = (pageReq.getPageNum() - 1) * pageReq.getPageSize();
        List<Resource> resources = resourceMapper.searchResourcesByName(
                resourceName,
                tag,
                offset,
                pageReq.getPageSize());
        Long total = resourceMapper.countSearchResourcesByName(resourceName, tag);
        return Result.buildSuccess(resources, total);
    }

    /**
     * 更新资源（只允许修改 resourceType, resourceDescription, 和 tag）
     * @param resourceId 资源ID
     * @param resourceUpdate 资源更新对象
     * @return 更新后的资源对象
     */
    @Override
    public Resource updateResource(
            Integer resourceId,
            Resource resourceUpdate) {
        Resource existingResource = resourceMapper.findById(resourceId);
        if (existingResource != null) {
            existingResource.setResourceType(resourceUpdate.getResourceType());
            existingResource.setResourceDescription(resourceUpdate.getResourceDescription());
            existingResource.setTag(resourceUpdate.getTag());
            resourceMapper.updateResource(existingResource);
        }
        return existingResource;
    }

    /**
     * 分页查询资源
     * @param pageReq 分页请求参数
     * @return 资源列表和总数
     */
    @Override
    public Result<List<Resource>> queryPage(ResourcePageReq pageReq) {
        int offset = (pageReq.getPageNum() - 1) * pageReq.getPageSize();
        List<Resource> resources = resourceMapper.queryPage(
                offset,
                pageReq.getPageSize());
        Long total = resourceMapper.countTotalResources();
        return Result.buildSuccess(resources, total);
    }
}